package com.tianji.upload.utils;

import cn.hutool.core.io.FileUtil;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.S3Object;
import com.tianji.upload.enum1.BucketNameEnum;
import com.tianji.upload.param.S3SdkPutParam;
import com.tianji.upload.param.S3SdkPutResult;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Paths;

/**
 * @ClassName S3SdkUtil
 * @Description
 * @Author yuwei
 * @Date 2020/7/16 19:35
 * @Version 1.0
 */
@Slf4j
public class S3SdkUtil {


    /**
     * 获取文件上传客户端
     * @return
     */
    private static AmazonS3 getClient() {
        ClientConfiguration config = new ClientConfiguration();
        //云鼎key
        String accessKey = "";
        //云鼎密钥
        String secretKey = "";
        //内网地域节点 todo 该地址配置到配置文件，上测试及生产时改为内网地址
//        String endpoint = "https://s3.cn-north-1.jdcloud-oss.com";
        String endpoint = "";
        //华北
        AwsClientBuilder.EndpointConfiguration endpointConfig = new AwsClientBuilder.EndpointConfiguration(endpoint, "cn-north-1");

        AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
        AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);
        return AmazonS3Client.builder()
                .withEndpointConfiguration(endpointConfig)
                .withClientConfiguration(config)
                .withCredentials(awsCredentialsProvider)
                .disableChunkedEncoding()
                .withPathStyleAccessEnabled(true)
                .build();
    }


    /**
     * 文件上传
     * @param putParam
     * @return
     */
    public static S3SdkPutResult put(S3SdkPutParam putParam){
        S3SdkPutResult putResult = new S3SdkPutResult();
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(putParam.getContentLength());
        String key = Paths.get(putParam.getFileName()).getFileName().toString();
        //上传文件流
        InputStream input = putParam.getInputStream();
        try {
            AmazonS3 s3 = getClient();
            s3.putObject(putParam.getBucketNameEnum().getBucket(), key, input, objectMetadata);
            log.info("Uploading {} to OSS bucket {}...\n", key, putParam.getBucketNameEnum().getBucket());

            putResult.setSuccess(true);
            putResult.setKey(key);
        } catch (AmazonServiceException e) {
            log.error("Uploading " + key + " to OSS bucket error", e);
            putResult.setSuccess(false);
            putResult.setKey(key);
        }finally {
            if(input != null){
                try {
                    input.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return putResult;
    }

    /**
     * 下载文件
     * @param bucketName 空间名称
     * @param key 文件名
     * @return
     */
    public static InputStream getObject(BucketNameEnum bucketName, String key){

        try {
            AmazonS3 s3 = getClient();
            S3Object o = s3.getObject(bucketName.getBucket(), key);
            return o.getObjectContent();
        } catch (AmazonServiceException e) {
            log.error("下载京东OSS文件异常",e);
        }
        return null;
    }


    public static void main(String[] args) {

//        File file = new File("C:\\Users\\Administrator\\Desktop\\短信平台\\短信平台项目情况.xlsx");
//        InputStream inputStream = IoUtil.toStream(file);
//        S3SdkPutParam putParam = new S3SdkPutParam(inputStream,BucketNameEnum.YEION_FILE,"短信平台项目情况.xlsx",file.length());
//        S3SdkPutResult putResult = put(putParam);

        InputStream inputStream1 =  getObject(BucketNameEnum.YEION_FILE,"333333333333");
        FileUtil.writeFromStream(inputStream1,"C:\\Users\\Administrator\\Desktop\\短信平台\\短信平台项目情况1111.xlsx");
    }

}
